package org.baicaixiaozhan.learncommon.util;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.springframework.http.HttpStatus;

/**
 * DESC:
 *
 * @since 2021-06-22
 * @author baicaixiaozhan
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = false)
public class ResultTemplate<T> {
    /**
     * 请求状态编码
     */
    private Integer statusCode;
    /**
     * 请求状态信息
     */
    private String message;
    /**
     * 数据
     */
    private T body;

    private ResultTemplate() {
        throw new AssertionError();
    }

    private ResultTemplate(Integer statusCode, String message, T body) {
        this.statusCode = statusCode;
        this.message = message;
        this.body = body;
    }

    private static <T> ResultTemplate<T> getResultTemplate(HttpStatus status, T body) {
        return new ResultTemplate<>(status.value(), status.name(), body);
    }

    private static <T> ResultTemplate<T> getResultTemplate(Integer statusCode, String message, T body) {
        return new ResultTemplate<>(statusCode, message, body);
    }

    public static <T> ResultTemplate<T>  ok() {
        return getResultTemplate(HttpStatus.OK, null);
    }

    public static <T> ResultTemplate<T>  ok(T body) {
        return getResultTemplate(HttpStatus.OK, body);
    }

    public static <T> ResultTemplate<T>  badRequest() {
        return getResultTemplate(HttpStatus.BAD_REQUEST, null);
    }

    public static <T> ResultTemplate<T>  badRequest(String message) {
        return getResultTemplate(HttpStatus.BAD_REQUEST.value(), message, null);
    }

    public static <T> ResultTemplate<T>  notFound() {
        return getResultTemplate(HttpStatus.NOT_FOUND, null);
    }

    public static <T> ResultTemplate<T>  internalServerError(String message) {
        return getResultTemplate(HttpStatus.INTERNAL_SERVER_ERROR.value(), message, null);
    }

    public static <T> ResultTemplate<T>  unauthorized() {
        return getResultTemplate(HttpStatus.UNAUTHORIZED, null);
    }


}
